﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FrmDuctIndependent : System.Windows.Forms.Form
	{
		public FrmDuctIndependent()
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{CD092F6F-8154-443C-A14D-5705A6662E85}", null);
			this.IsInit = true;
			this.Init();
			this.IsInit = false;
		}

		private void Init()
		{
			this.readDefaultValue();
		}

		private bool BeUseSettingPrefix()
		{
			bool result = false;
			IEnumerator enumerator = this.cbmLevel.Items.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.ToString() == this.MarkSetting.OffsetPrefix)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private void readDefaultValue()
		{
			this.setting = new UserSetting();
			this.MarkSetting = this.setting.GetDuctMarkSetting();
			this.IndependentSetting = this.setting.ReadDuctIndependentSetting();
			this.commFont = this.setting.GetFontSetting();
			this.SetIndependentSetting();
			this.SetPictureShow();
		}

		private void writeDefaultValue()
		{
			this.IndependentSetting = new DuctIndependentData
			{
				IsCode = this.chbCode.Checked,
				IsSize = this.chbSize.Checked,
				IsLevel = this.chbLevel.Checked,
				IsAir = this.chbAir.Checked,
				IsSpeed = this.chbSpeed.Checked,
				StrLevel = this.cbmLevel.SelectedItem.ToString(),
				IsSingle = this.rboSingle.Checked,
				IsLine = this.rboLine.Checked,
				StrLine = this.GetLine()
			};
			this.setting.WriteDuctIndependentSetting(this.IndependentSetting);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void radioButton2_CheckedChanged(object sender, EventArgs e)
		{
			this.rboLine.Enabled = !this.rboMultiple.Checked;
			this.SetPictureShow();
			this.IsChangeSel = true;
			if (this.rboMultiple.Checked && !this.rboNoLine.Checked)
			{
				this.rboNoLine.Checked = true;
				return;
			}
			this.ClickESC();
		}

		private void ClickESC()
		{
			if (this.IsInit)
			{
				return;
			}
			if (DuctIndependentCommond.hWndRevit != null)
			{
				int bVk = 27;
				int dwFlags = 0;
				int dwFlags2 = 2;
				int wMsg = 7;
				FrmDuctIndependent.SendMessage(DuctIndependentCommond.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				FrmDuctIndependent.keybd_event((byte)bVk, 0, dwFlags, 0);
				FrmDuctIndependent.keybd_event((byte)bVk, 0, dwFlags2, 0);
			}
		}

		private void radioButton4_CheckedChanged(object sender, EventArgs e)
		{
			this.rboUpLine.Enabled = !this.rboLine.Checked;
			this.rboOnLine.Enabled = !this.rboLine.Checked;
			this.rboDownLine.Enabled = !this.rboLine.Checked;
			this.SetPictureShow();
			this.IsChangeSel = true;
			this.ClickESC();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			FormTotalMarkSetting formTotalMarkSetting = new FormTotalMarkSetting(ShowMarkType.Duct);
			formTotalMarkSetting.SetDuctMarkPrefixSelectedItem(this.cbmLevel.SelectedItem.ToString());
			formTotalMarkSetting.ShowDialog();
			if (formTotalMarkSetting.DialogResult == DialogResult.OK)
			{
				this.setting = new UserSetting();
				this.MarkSetting = this.setting.GetDuctMarkSetting();
				this.SetPictureShow();
				this.cbmLevel.SelectedItem = formTotalMarkSetting.GetDuctPrefixText();
			}
		}

		private void checkBox3_CheckedChanged(object sender, EventArgs e)
		{
			this.cbmLevel.Enabled = this.chbLevel.Checked;
			this.SetPictureShow();
		}

		private void FrmDuctIndependent_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.writeDefaultValue();
			this.ClickESC();
		}

		private void rboLine_CheckedChanged(object sender, EventArgs e)
		{
			this.SetPictureShow();
		}

		private void chb_CheckedChanged(object sender, EventArgs e)
		{
			this.SetPictureShow();
		}

		private void cbmLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SetPictureShow();
		}

		private void SetPictureShow()
		{
			if (this.MarkSetting == null)
			{
				return;
			}
			if (this.rboSingle.Checked)
			{
				if (this.rboNoLine.Checked)
				{
					if (this.rboUpLine.Checked)
					{
						this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.UpLine;
						return;
					}
					if (this.rboOnLine.Checked)
					{
						this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.OnLine;
						return;
					}
					if (this.rboDownLine.Checked)
					{
						this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.DownLine;
						return;
					}
				}
				else
				{
					if (this.chbAir.Checked || this.chbSpeed.Checked)
					{
						this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.Air;
						return;
					}
					this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.Size;
					return;
				}
			}
			else
			{
				if (this.rboUpLine.Checked)
				{
					this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.MutiUp;
					return;
				}
				if (this.rboOnLine.Checked)
				{
					this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.Muti;
					return;
				}
				if (this.rboDownLine.Checked)
				{
					this.pictureBox1.Image = YArchitech.HVAC.Properties.Resources.MutiDown;
				}
			}
		}

		private void SetIndependentSetting()
		{
			if (this.IndependentSetting != null)
			{
				this.chbCode.Checked = this.IndependentSetting.IsCode;
				this.chbSize.Checked = this.IndependentSetting.IsSize;
				this.chbLevel.Checked = this.IndependentSetting.IsLevel;
				this.chbAir.Checked = this.IndependentSetting.IsAir;
				this.chbSpeed.Checked = this.IndependentSetting.IsSpeed;
				this.cbmLevel.SelectedItem = (string.IsNullOrEmpty(this.IndependentSetting.StrLevel) ? "顶" : (this.BeUseSettingPrefix() ? this.MarkSetting.OffsetPrefix : this.IndependentSetting.StrLevel));
				if (this.IndependentSetting.StrLine == "线上")
				{
					this.rboUpLine.Checked = true;
					this.rboDownLine.Checked = false;
					this.rboOnLine.Checked = false;
				}
				else if (this.IndependentSetting.StrLine == "线下")
				{
					this.rboUpLine.Checked = false;
					this.rboDownLine.Checked = true;
					this.rboOnLine.Checked = false;
				}
				else
				{
					this.rboUpLine.Checked = false;
					this.rboDownLine.Checked = false;
					this.rboOnLine.Checked = true;
				}
				if (this.IndependentSetting.IsSingle)
				{
					this.rboSingle.Checked = true;
					this.rboMultiple.Checked = false;
				}
				else
				{
					this.rboSingle.Checked = false;
					this.rboMultiple.Checked = true;
				}
				if (this.IndependentSetting.IsLine)
				{
					this.rboNoLine.Checked = false;
					this.rboLine.Checked = true;
					return;
				}
				this.rboNoLine.Checked = true;
				this.rboLine.Checked = false;
			}
		}

		private string GetLine()
		{
			if (this.rboUpLine.Checked)
			{
				return "线上";
			}
			if (this.rboDownLine.Checked)
			{
				return "线下";
			}
			return "线中";
		}

		private void FrmDuctIndependent_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.writeDefaultValue();
				base.Close();
			}
		}

		private void FrmDuctIndependent_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.KeyPreview = true;
		}

		private void FrmDuctIndependent_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private UserSetting setting;

		public DuctMarkSet MarkSetting;

		public DuctIndependentData IndependentSetting;

		public bool IsChangeSel;

		public CommonFont commFont;

		public bool IsInit;

		private ControlInfoManager m_thisManger;
	}
}
